package com.smartStatus.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.smartStatus.admin.service.GradeClassService;
import com.smartStatus.admin.service.SysClassesService;
import com.smartStatus.api.entity.GradeClass;
import com.smartStatus.api.entity.SysClasses;
import com.smartStatus.api.vo.SysClassesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartStatus.core.util.R;

/**
 * 系统班级表 Controller
 *
 * @author lby
 * @since 2021-03-06
 */
@RestController
@Slf4j
@Api(value = "SysClassesController", tags = "SysClasses管理")
@AllArgsConstructor
@RequestMapping("/SysClasses")
public class SysClassesController {

    private final SysClassesService sysClassesService;
    private final GradeClassService gradeClassService;

    /**
     * 分页数据
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "公共参数分页数据")
    @GetMapping("/page")
    public R sysClassesPage(Page<SysClasses> page, SysClasses sysClasses) {
        QueryWrapper<SysClasses> query = Wrappers.query();
        return R.ok(sysClassesService.page(page, query));
    }


    /**
     * 列表数据
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "公共参数列表")
    @GetMapping("/list")
    public R sysClassesList() {
        return R.ok(sysClassesService.list());
    }


    /**
     * 编辑数据
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "编辑公共参数")
    @ApiImplicitParam(name = "SysClasses", value = "SysClasses实体", required = true, dataType = "SysClasses")
    @PutMapping("/edit")
    public R sysClassesUpdate(@RequestBody SysClasses sysClasses) {
        return R.ok(sysClassesService.updateById(sysClasses));
    }


    /**
     * 删除数据
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "删除公共参数")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @PostMapping("/del-class")
    public R sysClassesDelete(@RequestBody SysClassesVO sysClassesVO) {
        sysClassesService.removeById(sysClassesVO.getId());
        QueryWrapper<GradeClass> query = Wrappers.query();
        query.eq("grade_id", sysClassesVO.getGradeId());
        query.eq("class_id", sysClassesVO.getId());
        return R.ok(gradeClassService.remove(query));
    }

    /**
     * 添加数据
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "添加公共参数")
    @ApiImplicitParam(name = "SysClasses", value = "SysClasses实体", required = true, dataType = "SysClasses")
    @PostMapping("/add")
    public R sysClassesInsert(@RequestBody SysClassesVO sysClassesVO) {
        sysClassesService.save(sysClassesVO);
        GradeClass gradeClass = new GradeClass();
        gradeClass.setClassId(sysClassesVO.getId());
        gradeClass.setGradeId(sysClassesVO.getGradeId());
        return R.ok(gradeClassService.save(gradeClass));
    }
}
